//
//  XMRequireView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/6.
//

#import "XMRequireView.h"

@interface XMRequireView ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIButton *ageBtn1;
@property (nonatomic, strong) UIButton *ageBtn2;
@property (nonatomic, strong) UIButton *ageStartBtn;
@property (nonatomic, strong) UIView *ageSelectLineView;
@property (nonatomic, strong) UIButton *ageEndBtn;

@property (nonatomic, assign) CGFloat startCenterX;
@property (nonatomic, assign) CGFloat endCenterX;

@property (nonatomic, strong) NSMutableArray *sexBtnArr;
@property (nonatomic, strong) NSMutableArray *heightBtnArr;
@property (nonatomic, strong) NSMutableArray *weightBtnArr;
@property (nonatomic, strong) NSMutableArray *starBtnArr;

@end

@implementation XMRequireView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEXA(0x464646, 0.46);
    [self addSubview:self.contentView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, KScaleWidth(10.0), self.width, KScaleWidth(46.0)) font:KBoldFontSize(17.0) textColor:kColorHEX(0x333333) textAlignment:NSTextAlignmentCenter];
    titleLabel.text = @"对TA的要求";
    [self.contentView addSubview:titleLabel];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(12.0) frame:CGRectMake(self.width - KScaleWidth(8.0) - KScaleWidth(55.0), titleLabel.y + (titleLabel.height - KScaleWidth(24.0))/2, KScaleWidth(55.0), KScaleWidth(24.0)) title:@"取消" titleColor:kColorHEX(0x333333)];
    [cancelBtn setRadius:cancelBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
    [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:cancelBtn];
    
    //性别
    UILabel *sexTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), titleLabel.maxY, self.width - KScaleWidth(15.0), KScaleWidth(42.0)) font:KFontSize(16.0) textColor:kColorHEX(0x666666)];
    sexTitleLabel.text = @"TA的性别";
    [_contentView addSubview:sexTitleLabel];
    NSArray *sexTitleArr = @[@"不限", @"男生", @"女生"];
    UIImage *selectImg = [UIImage gradientColorImageFromColors:@[kColorHEX(0xFFE44E),  kColorHEX(0xFFD800)] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(KScaleWidth(101.0), KScaleWidth(34.0))];
    for (int i = 0; i < sexTitleArr.count; i ++) {
        UIButton *sexBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(KScaleWidth(15.0) + (KScaleWidth(101.0) + KScaleWidth(10.0))*i, sexTitleLabel.maxY, KScaleWidth(101.0), KScaleWidth(34.0)) title:sexTitleArr[i] titleColor:kColorHEX(0x999999)];
        [sexBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateSelected];
        [sexBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF3F3F3)] forState:UIControlStateNormal];
        [sexBtn setBackgroundImage:selectImg forState:UIControlStateSelected];
        [sexBtn setCornerRadius:KScaleWidth(8.0)];
        [sexBtn addTarget:self action:@selector(sexSelectAction:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:sexBtn];
        [self.sexBtnArr addObject:sexBtn];
        if (i == 0) {
            sexBtn.selected = YES;
        }
    }
    //年龄
    UILabel *ageTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), sexTitleLabel.maxY + KScaleWidth(35.0), sexTitleLabel.width, KScaleWidth(42.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    ageTitleLabel.text = @"TA的年龄";
    [_contentView addSubview:ageTitleLabel];
    _contentView.y = self.height;
    [_contentView addSubview:self.ageBtn1];
    [_contentView addSubview:self.ageBtn2];
    _ageBtn1.y = ageTitleLabel.maxY;
    _ageBtn2.y = ageTitleLabel.maxY;
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(_ageBtn1.maxX + KScaleWidth(4.0), _ageBtn1.y + _ageBtn1.height/2 - 1, KScaleWidth(10.0), 1)];
    lineView.backgroundColor = kColorHEX(0xE5E5E5);
    [_contentView addSubview:lineView];
    
    UIView *ageLineView = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), _ageBtn1.maxY + KScaleWidth(13.0), self.width - KScaleWidth(10.0)*2, KScaleWidth(6.0))];
    ageLineView.backgroundColor = kColorHEX(0xE7E7E7);
    [_contentView addSubview:ageLineView];
    [_contentView addSubview:self.ageSelectLineView];
    [_contentView addSubview:self.ageStartBtn];
    [_contentView addSubview:self.ageEndBtn];
    _ageStartBtn.center = CGPointMake(ageLineView.x, ageLineView.center.y);
    _ageEndBtn.center = CGPointMake(ageLineView.x + KScaleWidth(30.0), ageLineView.center.y);
    _ageSelectLineView.frame = CGRectMake(_ageStartBtn.maxX, ageLineView.y, _ageEndBtn.x - _ageStartBtn.maxX, ageLineView.height);
    UIPanGestureRecognizer *startPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(ageStartSelectAction:)];
    startPan.delegate = self;
    [_ageStartBtn addGestureRecognizer:startPan];
    UIPanGestureRecognizer *endPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(ageEndSelectAction:)];
    endPan.delegate = self;
    [_ageStartBtn addGestureRecognizer:startPan];
    [_ageEndBtn addGestureRecognizer:endPan];
    _startCenterX = _ageStartBtn.centerX;
    _endCenterX = _ageEndBtn.centerX;
    
    //身高
    UILabel *heightTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), ageLineView.maxY + KScaleWidth(3.0), self.width - KScaleWidth(15.0), KScaleWidth(42.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    heightTitleLabel.text = @"TA的身高（可多选）";
    [_contentView addSubview:heightTitleLabel];
    NSArray *heightTitleArr = @[@"155以下", @"155-160", @"160-170", @"170-175", @"175-180", @"180以上"];
    for (int i = 0; i < heightTitleArr.count; i ++) {
        UIButton *heightBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(KScaleWidth(15.0) + (KScaleWidth(101.0) + KScaleWidth(10.0))*(i%3), heightTitleLabel.maxY + (KScaleWidth(34.0) + KScaleWidth(10.0))*(i/3), KScaleWidth(101.0), KScaleWidth(34.0)) title:heightTitleArr[i] titleColor:kColorHEX(0x999999)];
        [heightBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateSelected];
        [heightBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF3F3F3)] forState:UIControlStateNormal];
        [heightBtn setBackgroundImage:selectImg forState:UIControlStateSelected];
        [heightBtn setCornerRadius:KScaleWidth(8.0)];
        [heightBtn addTarget:self action:@selector(heightSelectAction:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:heightBtn];
        [self.heightBtnArr addObject:heightBtn];
    }
    
    //体重
    UILabel *weightTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), heightTitleLabel.maxY + KScaleWidth(78.0), self.width - KScaleWidth(15.0), KScaleWidth(42.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    weightTitleLabel.text = @"TA的体重（可多选）";
    [_contentView addSubview:weightTitleLabel];
    NSArray *weightTitleArr = @[@"50以下", @"50-55", @"55-60", @"60-65", @"65-70", @"70以上"];
    for (int i = 0; i < weightTitleArr.count; i ++) {
        UIButton *weightBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(KScaleWidth(15.0) + (KScaleWidth(101.0) + KScaleWidth(10.0))*(i%3), weightTitleLabel.maxY + (KScaleWidth(34.0) + KScaleWidth(10.0))*(i/3), KScaleWidth(101.0), KScaleWidth(34.0)) title:weightTitleArr[i] titleColor:kColorHEX(0x999999)];
        [weightBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateSelected];
        [weightBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF3F3F3)] forState:UIControlStateNormal];
        [weightBtn setBackgroundImage:selectImg forState:UIControlStateSelected];
        [weightBtn setCornerRadius:KScaleWidth(8.0)];
        [weightBtn addTarget:self action:@selector(weightSelectAction:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:weightBtn];
        [self.weightBtnArr addObject:weightBtn];
    }
    
    //星座
    UILabel *starTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), weightTitleLabel.maxY + KScaleWidth(78.0), self.width - KScaleWidth(15.0), KScaleWidth(42.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    starTitleLabel.text = @"TA的星座（可多选）";
    [_contentView addSubview:starTitleLabel];
    NSArray *starTitleArr = @[@"巨蟹座", @"白羊座", @"金牛座", @"双子座", @"天蝎座", @"狮子座", @"处女座", @"天秤座", @"双鱼座", @"射手座", @"摩羯座", @"水瓶座"];
    for (int i = 0; i < starTitleArr.count; i ++) {
        UIButton *starBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(KScaleWidth(15.0) + (KScaleWidth(101.0) + KScaleWidth(10.0))*(i%3), starTitleLabel.maxY + (KScaleWidth(34.0) + KScaleWidth(10.0))*(i/3), KScaleWidth(101.0), KScaleWidth(34.0)) title:starTitleArr[i] titleColor:kColorHEX(0x999999)];
        [starBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateSelected];
        [starBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF3F3F3)] forState:UIControlStateNormal];
        [starBtn setBackgroundImage:selectImg forState:UIControlStateSelected];
        [starBtn setCornerRadius:KScaleWidth(8.0)];
        [starBtn addTarget:self action:@selector(starSelectAction:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:starBtn];
        [self.starBtnArr addObject:starBtn];
    }
    
    UIButton *resetBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(17.0) frame:CGRectMake(KScaleWidth(15.0), starTitleLabel.maxY + KScaleWidth(187.0), KScaleWidth(140.0), KScaleWidth(44.0)) title:@"重置" titleColor:kColorHEX(0xD1B100)];
    resetBtn.backgroundColor = kColorHEX(0xFFF5BB);
    [resetBtn setCornerRadius:resetBtn.height/2];
    [resetBtn addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:resetBtn];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake(self.width - KScaleWidth(28.0) - KScaleWidth(180.0), resetBtn.y, KScaleWidth(180.0), KScaleWidth(44.0)) title:@"确定" titleColor:kColorHEX(0x333333)];
    [confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
    [confirmBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.05, 0.37)];
    [confirmBtn setCornerRadius:confirmBtn.height/2];
    [_contentView addSubview:confirmBtn];
    
    self.alpha = 0;
}

- (void)cancelAction:(UIButton *)button {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.contentView.y = weakSelf.height;
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

//年龄
- (void)sexSelectAction:(UIButton *)button {
    if (button.selected) {
        return;
    }
    for (UIButton *sexBtn in self.sexBtnArr) {
        sexBtn.selected = NO;
    }
    button.selected = YES;
}

//身高
- (void)heightSelectAction:(UIButton *)button {
    button.selected = !button.selected;
}

//体重
- (void)weightSelectAction:(UIButton *)button {
    button.selected = !button.selected;
}

//星座
- (void)starSelectAction:(UIButton *)button {
    button.selected = !button.selected;
}

//- (void)age1Action:(UIButton *)button {
//    button.selected = YES;
//    _ageBtn2.selected = NO;
//}
//
//- (void)age2Action:(UIButton *)button {
//    button.selected = YES;
//    _ageBtn1.selected = NO;
//}

//年龄选择
- (void)ageStartSelectAction:(UIPanGestureRecognizer *)pan {
    CGPoint movePoint = [pan translationInView:self.contentView];
    CGFloat  centerX = _startCenterX + movePoint.x;
    _ageStartBtn.centerX = MAX(KScaleWidth(10.0), MIN(_ageEndBtn.centerX, centerX));
    
    _ageSelectLineView.x = _ageStartBtn.centerX;
    _ageSelectLineView.width = _ageEndBtn.centerX - _ageStartBtn.centerX;
    
    NSInteger startAge = 18 + round((_ageStartBtn.centerX - KScaleWidth(10.0))/(self.width - KScaleWidth(10.0)*2)*(100 - 18));
    [_ageBtn1 setTitle:[NSString stringWithFormat:@"%ld", startAge] forState:UIControlStateNormal];
    if (pan.state == UIGestureRecognizerStateEnded) {
        _startCenterX = _ageStartBtn.centerX;
    }
}

//年龄选择
- (void)ageEndSelectAction:(UIPanGestureRecognizer *)pan {
    CGPoint movePoint = [pan translationInView:self.contentView];
    CGFloat centerX = _endCenterX + movePoint.x;
    _ageEndBtn.centerX = MIN(self.width - KScaleWidth(10.0), MAX(_ageStartBtn.centerX, centerX));
    
    _ageSelectLineView.width = _ageEndBtn.centerX - _ageStartBtn.centerX;
    
    NSInteger endAge = 18 + round((_ageEndBtn.centerX - KScaleWidth(10.0))/(self.width - KScaleWidth(10.0)*2)*(100 - 18));
    [_ageBtn2 setTitle:[NSString stringWithFormat:@"%ld", endAge] forState:UIControlStateNormal];
    if (pan.state == UIGestureRecognizerStateEnded) {
        _endCenterX = _ageEndBtn.centerX;
    }
}

//重置
- (void)resetAction:(UIButton *)button {
    for (UIButton *itemBtn in self.sexBtnArr) {
        itemBtn.selected = NO;
    }
    for (UIButton *itemBtn in self. heightBtnArr) {
        itemBtn.selected = NO;
    }
    for (UIButton *itemBtn in self.weightBtnArr) {
        itemBtn.selected = NO;
    }
    for (UIButton *itemBtn in self.starBtnArr) {
        itemBtn.selected = NO;
    }
    [_ageBtn1 setTitle:@"18" forState:UIControlStateNormal];
    [_ageBtn2 setTitle:@"26" forState:UIControlStateNormal];
    _ageStartBtn.centerX = KScaleWidth(10.0);
    _ageEndBtn.centerX = KScaleWidth(10.0) + KScaleWidth(30.0);
    _ageSelectLineView.frame = CGRectMake(_ageStartBtn.centerX, _ageSelectLineView.y, _ageEndBtn.centerX - _ageStartBtn.centerX, _ageSelectLineView.height);
}

//确定
- (void)confirmAction:(UIButton *)button {
    NSString *sexStr = @""; //性别
    for (UIButton *itemBtn in self.sexBtnArr) {
        if (itemBtn.selected) {
            sexStr = itemBtn.titleLabel.text;
        }
    }
    NSString *heightStr = @""; //身高
    for (UIButton *itemBtn in self.heightBtnArr) {
        if (itemBtn.selected) {
            if (heightStr.length > 0) {
                heightStr = [NSString stringWithFormat:@"%@,%@", heightStr, itemBtn.titleLabel.text];
            }
            else {
                heightStr = itemBtn.titleLabel.text;
            }
        }
    }
    
    NSString *weightStr = @""; //体重
    for (UIButton *itemBtn in self.weightBtnArr) {
        if (itemBtn.selected) {
            if (weightStr.length > 0) {
                weightStr = [NSString stringWithFormat:@"%@,%@", weightStr, itemBtn.titleLabel.text];
            }
            else {
                weightStr = itemBtn.titleLabel.text;
            }
        }
    }
    
    NSString *starStr = @"";  //星座
    for (UIButton *itemBtn in self.starBtnArr) {
        if (itemBtn.selected) {
            if (starStr.length > 0) {
                starStr = [NSString stringWithFormat:@"%@,%@", starStr, itemBtn.titleLabel.text];
            }
            else {
                starStr = itemBtn.titleLabel.text;
            }
        }
    }
    NSString *ageStr = [NSString stringWithFormat:@"%@-%@", _ageBtn1.titleLabel.text, _ageBtn2.titleLabel.text];
    self.requireBlock ? self.requireBlock(sexStr, ageStr, heightStr, weightStr, starStr) : nil;
    [self cancelAction:nil];
}

- (void)show {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.alpha = 1;
        weakSelf.contentView.y = KScaleWidth(50.0);
    }];
}

#pragma mark - Init
- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, KScaleWidth(50.0), self.width, self.height - KScaleWidth(50.0))];
        _contentView.backgroundColor = kColorHEX(0xffffff);
        [_contentView setPartCornerWithRectCorner:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:KScaleWidth(12.0)];
    }
    return _contentView;
}

- (NSMutableArray *)sexBtnArr {
    if (!_sexBtnArr) {
        _sexBtnArr = [NSMutableArray array];
    }
    return _sexBtnArr;
}

- (UIButton *)ageBtn1 {
    if (!_ageBtn1) {
        _ageBtn1 = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(KScaleWidth(15.0), 0, KScaleWidth(101.0), KScaleWidth(34.0)) title:@"18" titleColor:kColorHEX(0x333333)];
        [_ageBtn1 setTitleColor:kColorHEX(0x333333) forState:UIControlStateSelected];
        [_ageBtn1 setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF3F3F3)] forState:UIControlStateNormal];
//        [_ageBtn1 addTarget:self action:@selector(age1Action:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _ageBtn1;
}

- (UIButton *)ageBtn2 {
    if (!_ageBtn2) {
        _ageBtn2 = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(KScaleWidth(130.0), 0, KScaleWidth(101.0), KScaleWidth(34.0)) title:@"26" titleColor:kColorHEX(0x333333)];
        [_ageBtn2 setTitleColor:kColorHEX(0x333333) forState:UIControlStateSelected];
        [_ageBtn2 setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF3F3F3)] forState:UIControlStateNormal];
//        [_ageBtn2 addTarget:self action:@selector(age2Action:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _ageBtn2;
}

- (UIButton *)ageStartBtn {
    if (!_ageStartBtn) {
        _ageStartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _ageStartBtn.frame = CGRectMake(KScaleWidth(10.0) - KScaleWidth(20.0)/2, 0, KScaleWidth(20.0), KScaleWidth(20.0));
        _ageStartBtn.backgroundColor = kColorHEX(0xffffff);
        [_ageStartBtn setRadius:_ageStartBtn.height/2 borderColor:kColorHEX(0xFFD800) borderWidth:1];
    }
    return _ageStartBtn;
}

- (UIView *)ageSelectLineView {
    if (!_ageSelectLineView) {
        _ageSelectLineView = [[UIView alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), 0, KScaleWidth(10.0), KScaleWidth(6.0))];
        _ageSelectLineView.backgroundColor = kColorHEX(0xFFD800);
    }
    return _ageSelectLineView;
}

- (UIButton *)ageEndBtn {
    if (!_ageEndBtn) {
        _ageEndBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _ageEndBtn.frame =  self.ageStartBtn.bounds;
        _ageEndBtn.backgroundColor = kColorHEX(0xffffff);
        [_ageEndBtn setRadius:_ageEndBtn.height/2 borderColor:kColorHEX(0xFFD800) borderWidth:1];
    }
    return _ageEndBtn;
}

- (NSMutableArray *)heightBtnArr {
    if (!_heightBtnArr) {
        _heightBtnArr = [NSMutableArray array];
    }
    return _heightBtnArr;
}

- (NSMutableArray *)weightBtnArr {
    if (!_weightBtnArr) {
        _weightBtnArr = [NSMutableArray array];
    }
    return _weightBtnArr;
}

- (NSMutableArray *)starBtnArr {
    if (!_starBtnArr) {
        _starBtnArr = [NSMutableArray array];
    }
    return _starBtnArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
